const fs = require("fs")
const csv=require('csvtojson');

function ConvertToTable(data, callBack) {
  data = data.toString();
  var table = new Array();
  var rows = new Array();
  rows = data.split("\n");
  for (var i = 0; i < rows.length; i++) {
    table.push(rows[i].split(","));
  }
  callBack(table);
}

function defaultTask(cb) {
  var fileStr = fs.readFileSync('test.csv', {encoding:'binary'});
  ConvertToTable(fileStr, function (table) {
    console.log(table);
  })

  const csvFilePath = "test.csv"
  csv()
  .fromFile(csvFilePath)
  .then((jsonObj)=>{
    jsonObj = JSON.stringify(jsonObj)
    fs.writeFile("./newData.json",jsonObj,'utf-8', function(err) {
      if(err) {
        return console.log(err);
      }

      console.log("The file was saved!");
    });
  })

  cb();
}

exports.default = defaultTask
